package com.langChain4j.model.factory;

import com.langChain4j.api.entity.AgentDialogSession;
import com.langChain4j.api.vo.AgentChatVo;
import com.langChain4j.conversation.service.AgentConversationService;
import dev.langchain4j.data.message.ChatMessageSerializer;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

import static dev.langchain4j.internal.Utils.getOrDefault;

/**
 * @author linjun
 * @date 2025/8/20 17:00
 * @description
 */
public abstract class AbsAgentLoader implements AgentLoader {

    @Autowired
    protected AgentConversationService conversationService;

    public static final String OPENAI_API_KEY = getOrDefault(System.getenv("DASHSCOPE_API_KEY"), "");

    /**
     * @Description: 追加会话消息内容
     * @Param:
     * @return:
     * @Author: linjun
     * @Date: 2025/9/11 11:20
     */
    protected void contractSessionMessage(AgentChatVo chatVo) {
        // 更新会话上下文（模拟）
        AgentDialogSession dialogSession = conversationService.queryBySessionId(chatVo.getSessionId());
        dialogSession.setContextMemory(ChatMessageSerializer.messagesToJson(chatVo.getHistoryMessage()));
        dialogSession.setUpdateTime(LocalDateTime.now());
        conversationService.updateById(dialogSession);
    }
}
